<?php
class RecordingController extends AppController{
    /**
     * set layout
     */
    public function beforeFilter() {
        parent::beforeFilter();
        $this->loadModel('Camera');
        $this->loadModel('Record');
        $this->set('title_for_layout', "camera");
        $this->menu_active = 'recording';
    }
    /**
     * 
     */
    public function index(){
        $cameras = $this->Camera->getAllCamera($this->request->data);
        //echo "<pre>";var_dump($cameras);die();
        $this->set('cameras', $cameras);
    }
    
    
    /**
     * 
     */
    public function startRecord($id = null){
        set_time_limit(0);
        if (!$id) {
            echo json_encode(array('error' => 1));
            exit();
        }

        $Camera = $this->Camera->find('first',array('conditions' => array('Camera.Id' => $id)));
        if (!$Camera || !isset($Camera['Camera']['duration']) || $Camera['Camera']['is_start'] != 0 || $Camera['Camera']['Off'] == 1) {
            echo json_encode(array('error' => 1));
            exit();
        }
        $this->Camera->updateAll(array('is_start' => 1), "Camera.Id = '".$id."'");
        $file_record = PATH_BACKGROUND."record.php";
        pclose(popen('start php -f "'.$file_record.'" '.$id,"r"));
        //exec('php -f "'.$file_record.'" '.$id.' > /dev/null &');
        echo json_encode(array('error' => 0));
        exit();
        /*
        $folder = Common::getFolderName($id);
        $time = 5;
        $flat = true;
        $i = 0;
        while($flat){
                $fileMp4 = date('Y_m_d_H_i_s').".mp4";
                $file = PATH_ROOT.'video'.DS.$folder.DS.$fileMp4;
                $url = 'http://'.$Camera['Camera']['UniIp'].':'.$Camera['Camera']['HttpPort'].'/cgi-bin/encoder?USER='.$Camera['Camera']['UserName'].'&PWD='.$Camera['Camera']['Password'].'&GET_STREAM';
                $cmd = 'ffmpeg -i "'.$url.'" -vcodec libx264 -qmin 1 -qmax 1 -r 10 -t '.$time.' "'.$file.'"';
                shell_exec($cmd);
                //save record
                $i++;
                echo "record:".$i."<br>";
                $fileName = 'File-'.$this->Record->getNextFile($id);
                $data = array(
                    'RecName' => $fileName,
                    'RecPath' => $folder.'/'.$fileMp4,
                    'CamId' => $id,
                    'CrtDt' => date('Y-m-d H:i:s'),
                    'UptDt' => date('Y-m-d H:i:s'),
                );
                $this->Record->create();
                $this->Record->save(array('Record' => $data));
                //new file
                $Camera = $this->Camera->find('first',array('conditions' => array('CamID' => $id)));
                if($Camera && isset($Camera['Camera']['duration']) && $Camera['Camera']['is_start'] != 0){
                    $time = 5;//$Camera['Camera']['duration'] * 60;
                }else{
                    $flat = false;
                }
        }
         * 
         */
        
    }
    
    /**
     * 
     */
    public function stop($id = null){
        $this->layout = false;
        if (!$id) {
            echo json_encode(array('error' => 1));
            exit();
        }

        $Camera = $this->Camera->find('first',array('conditions' => array('Camera.Id' => $id)));
        if (!$Camera) {
            echo json_encode(array('error' => 1));
            exit();
        }
        $this->Camera->updateAll(array('is_start' => 0), "Camera.Id = '".$id."'");
        echo json_encode(array('error' => 0));
        exit();
        
    }
    
    
}

